class Solution {
public:
    int findLHS(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int ret = 0;
        int begin = 0;
        for (int end = 0; end < nums.size(); ++end)
        {
            while (nums[end] - nums[begin] > 1)
            {
                begin++;
            }
            if (nums[end] - nums[begin] == 1)
            {
                ret = (ret > (end - begin + 1) ? ret : (end - begin + 1));
            }
        }
        return ret;
    }
};